package aom.ju.ss.utils

import android.app.Activity
import android.content.Intent
import android.graphics.Bitmap
import android.graphics.Paint.STRIKE_THRU_TEXT_FLAG
import android.graphics.Typeface
import android.text.TextUtils
import android.view.View
import android.widget.Button
import android.widget.ImageView
import android.widget.TextView
import androidx.databinding.BindingAdapter
import aom.ju.ss.R
import aom.ju.ss.mode.Shop
import aom.ju.ss.mode.VideoListData
import com.bumptech.glide.Glide
import com.bumptech.glide.load.resource.bitmap.CircleCrop
import com.bumptech.glide.load.resource.bitmap.RoundedCorners
import com.bumptech.glide.request.RequestOptions


object BindingImgUtils {

    @JvmStatic
    @BindingAdapter("calculateDistance")
    fun calculateDistance(imageView: TextView, info: Shop?) {

        if (info == null) {
            return
        }

        val lat: String? = SpUtils.getString("lat")
        val lng: String? = SpUtils.getString("lng")
        if (lng != null && lat != null && info.lng != null && "0.0" != lat && info.lat != null && "0.0" != lng &&
            lng.toDoubleOrNull() != null && lat.toDoubleOrNull() != null && info.lng.toDoubleOrNull() != null && info.lat.toDoubleOrNull() != null
        ) {
            val distance = LocationUtils.getDistance(
                lng.toDouble(),
                lat.toDouble(),
                info.lng.toDouble(),
                info.lat.toDouble()
            )
            val s = LocationUtils.distanceFormat(distance)
            imageView.text = "距您$s"
        } else {
            imageView.text = "距您50.1公里"//假数据
        }
    }


    @JvmStatic
    @BindingAdapter("android:src")
    fun setSrc(view: ImageView, bitmap: Bitmap?) {
        view.setImageBitmap(bitmap)
    }

    @JvmStatic
    @BindingAdapter("android:src")
    fun setSrc(view: ImageView, resId: Int) {
        view.setImageResource(resId)
    }

    /**
     * 原形图片
     *
     * @param imageView
     * @param url
     */
    @JvmStatic
    @BindingAdapter("imgRoundHead")
    fun imgRoundHead(imageView: ImageView, url: String?) {
        Glide.with(imageView.context)
            .load(url)
            .placeholder(R.drawable.ic_default_head_user)
            .fallback(R.drawable.ic_default_head_user)
            .error(R.drawable.ic_default_head_user)
            .apply(RequestOptions.bitmapTransform(CircleCrop()))
            .into(imageView)
    }


    /**
     * 原形图片
     *
     * @param imageView
     * @param url
     */
    @JvmStatic
    @BindingAdapter("imgRound")
    fun imgRound(imageView: ImageView, url: String?) {
        if (TextUtils.isEmpty(url)) {
            return
        }
        Glide.with(imageView.context)
            .load(url)
            .error(R.drawable.default_img)
            .placeholder(R.drawable.default_img)
            .fallback(R.drawable.default_img)
            .apply(RequestOptions.bitmapTransform(CircleCrop()))
            .into(imageView)
    }


    /**
     * 原形图片
     *
     * @param imageView
     * @param url
     */

    @JvmStatic
    @BindingAdapter("imgRoundres")
    fun imgRoundres(imageView: ImageView, url: Int) {
        Glide.with(imageView.context)
            .load(url)
            .apply(RequestOptions.bitmapTransform(CircleCrop()))
            .error(R.drawable.default_img)
            .placeholder(R.drawable.default_img)
            .fallback(R.drawable.default_img)
            .into(imageView)
    }

    @JvmStatic
    @BindingAdapter("GoodsImg")
    fun GoodsImg(imageView: ImageView, url: String?) {
        if (TextUtils.isEmpty(url)) {
            Glide.with(imageView.context)
                .load(R.drawable.goods_def)
                .into(imageView)
            return
        }
        Glide.with(imageView.context)
            .load(url)
            .error(R.drawable.goods_def)
            .placeholder(R.drawable.goods_def)
            .fallback(R.drawable.goods_def)
            .into(imageView)
    }

    /**
     * 圆角
     *
     * @param imageView
     * @param url
     */
    @JvmStatic
    @BindingAdapter("imgFillet")
    fun imgFillet(imageView: ImageView, url: String?) {
        if (TextUtils.isEmpty(url)) {
            Glide.with(imageView.context)
                .load(R.drawable.default_img)
                .into(imageView)
            return
        }
        var radius: Int
        val uris: String
        if (url!!.contains("@@@")) {
            uris = url.split("@@@".toRegex()).toTypedArray()[0]
            radius = url.split("@@@".toRegex()).toTypedArray()[1].toInt()
            Glide.with(imageView.context)
                .load(uris)
                .apply(RequestOptions.bitmapTransform(RoundedCorners(radius.toPx))) //圆角半径
                .error(R.drawable.default_img)
                .placeholder(R.drawable.default_img)
                .fallback(R.drawable.default_img)
                .into(imageView)
        } else {
            uris = url
            Glide.with(imageView.context)
                .load(uris)
                .error(R.drawable.default_img)
                .placeholder(R.drawable.default_img)
                .fallback(R.drawable.default_img)
                .into(imageView)
        }

    }

    @JvmStatic
    @BindingAdapter("jiacui")
    fun jiacui(tv: TextView, isc: Boolean) {
        if (isc) {
            tv.typeface = Typeface.defaultFromStyle(Typeface.BOLD);//加粗
            tv.postInvalidate()
        } else {
            tv.typeface = Typeface.defaultFromStyle(Typeface.NORMAL);//常规
            tv.postInvalidate()
        }

    }

    @JvmStatic
    @BindingAdapter("xiahuaxian")
    fun xiahuaxian(tv: TextView, isc: Boolean) {
        if (isc) {
            if (isc) {
                tv.xhx()
            } else {
                tv.paint.flags = STRIKE_THRU_TEXT_FLAG

            }
        }

    }

    @JvmStatic
    @BindingAdapter("zhonghuaxian")
    fun zhonghuaxian(tv: TextView, isc: Boolean) {
        if (isc) {
            if (isc) {
                tv.zhx()
            } else {
                tv.paint.flags = STRIKE_THRU_TEXT_FLAG

            }
        }

    }

    /**
     * 圆角
     *
     * @param imageView
     * @param url
     */
    @JvmStatic
    @BindingAdapter("imgFilletG")
    fun imgFilletG(imageView: ImageView, url: String?) {
        if (TextUtils.isEmpty(url)) {

            return
        }
        var radius: Int
        val uris: String
        if (url!!.contains("@@@")) {
            uris = url.split("@@@".toRegex()).toTypedArray()[0]
            radius = url.split("@@@".toRegex()).toTypedArray()[1].toInt()
            Glide.with(imageView.context)
                .load(uris)
                .apply(RequestOptions.bitmapTransform(RoundedCorners(radius.toPx))) //圆角半径
                .error(R.drawable.default_img)
                .placeholder(R.drawable.default_img)
                .fallback(R.drawable.default_img)
                .into(imageView)
        } else {
            uris = url
            Glide.with(imageView.context)
                .load(uris)
                .error(R.drawable.default_img)
                .placeholder(R.drawable.default_img)
                .fallback(R.drawable.default_img)
                .into(imageView)
        }

    }

    @JvmStatic
    @BindingAdapter("shopone")
    fun shopone(imageView: ImageView, info: VideoListData?) {
        var u = ""
        if (info?.goods != null && info.goods!!.isNotEmpty()) {
            u = info.goods!![0].image!!
            imageView.VIV(true)
        }
//        else {
//            if (info?.newList?.picArr != null && info.newList!!.picArr.size >= 1) {
//                u = info.newList!!.picArr[0]
//                imageView.VIV(true)
//            } else {
//                imageView.VIV(false)
//            }
//        }

        if (u.isEmpty()) {
            return
        }
        Glide.with(imageView.context)
            .load(u)
            .error(R.drawable.default_img)
            .placeholder(R.drawable.default_img)
            .fallback(R.drawable.default_img)
            .into(imageView)
    }

    @JvmStatic
    @BindingAdapter("calculateDistance2")
    fun calculateDistance2(imageView: TextView, info: Shop?) {

        if (info == null) {
            return
        }

        val lat: String? = SpUtils.getString("lat")
        val lng: String? = SpUtils.getString("lng")
        if (lng != null && lat != null && info.lng != null && "0.0" != lat && info.lat != null && "0.0" != lng && info.lng!!.toDoubleOrNull() != null && info.lat!!.toDoubleOrNull() != null) {
            val distance = LocationUtils.getDistance(
                lng.toDouble(),
                lat.toDouble(),
                info.lng.toDouble(),
                info.lat.toDouble()
            )
            val s = LocationUtils.distanceFormat(distance)
            imageView.text = "距您$s|导航到店"
        } else {
            imageView.text = "距您50.1公里|导航到店"//假数据
        }
    }


    @JvmStatic
    @BindingAdapter("shopone1")
    fun shopone1(imageView: ImageView, info: VideoListData?) {
        var u = ""
        if (info?.goods != null && info.goods!!.isNotEmpty()) {
            if (info.goods!!.size >= 2) {
                u = info.goods!![1].image!!
                imageView.VIV(true)
            } else {
                imageView.VIV(false)
            }
        }

        if (u.isEmpty()) {
            return
        }
        Glide.with(imageView.context)
            .load(u)
            .error(R.drawable.default_img)
            .placeholder(R.drawable.default_img)
            .fallback(R.drawable.default_img)
            .into(imageView)
    }

    val imgs = arrayOf(
        R.drawable.ppsp,
        R.drawable.ppsp,
        R.drawable.kjth,
        R.drawable.jqhd,
        R.drawable.yhhw,
        R.drawable.hdcx
    )
//    状态  1下单成功  2已支付  3已发货  4已签收 7已退款（订单商品全部已退）  8已完成  9已取消
    val types = arrayOf(
        "", "待支付", "待发货", "待签收", "待评论", "", "","已退款（订单商品全部已退）","已完成","已取消",""
        )

    @JvmStatic
    @BindingAdapter("OrderTypeString")
    fun OrderTypeString(imageView: TextView, type: Int) {
        imageView.text = types[type]

    }

    @JvmStatic
    @BindingAdapter("goodsTypeImg")
    fun goodsTypeImg(imageView: ImageView, type: String) {

        LogUtils.e(type)
        Glide.with(imageView.context)
            .load(imgs[type.toInt()])
            .into(imageView)


//        "type": "订单类型  1普通订单  2砍价订单  3抢券订单  4易货订单  5易货促销订单",

    }
}